package com.etpjava.action;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.etpjava.model.Manager;
import com.etpjava.service.LoginService;
import com.etpjava.service.impl.LoginServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3919802960581729121L;
	private String username;
	private String password;
	Log logger=LogFactory.getLog(LoginAction.class);
	
	LoginService ls=new LoginServiceImpl();

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String login(){
		logger.info("登录开始了============="+username+password);
		Map session=ActionContext.getContext().getSession();
		//重复登陆判断
		String loginresult=(String)session.get("username");
		if (null!=loginresult&&!loginresult.isEmpty()) {
			return SUCCESS;
		}
		Manager mg=ls.query(username,password);
		if(null != mg){
			//登陆帐号
			session.put("username", String.valueOf(mg.getAccount()));
			//管理员级别
			session.put("le",String.valueOf(mg.getLevel()));	
			return SUCCESS;
		}else{
			addFieldError("un", "用户名或密码错误");
			return "fail";
		}
	}
	
	public String logout(){
			logger.info("退出=============");
			@SuppressWarnings("rawtypes")
			Map session=ActionContext.getContext().getSession();
			if (null !=session.get("username")) {
				session.remove("username");
				session.remove("le");
			}
			return LOGIN;
		
	}
	
	
}
